package com.sec.etech.bpm.cust.controller;


import com.github.pagehelper.Page;
import com.sec.etech.bpm.cust.dto.EtechBpmTask;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.openbpm.base.api.query.QueryFilter;
import org.openbpm.base.api.query.QueryOP;
import org.openbpm.base.api.response.impl.ResultMsg;
import org.openbpm.base.core.util.StringUtil;
import org.openbpm.base.db.model.page.PageResult;
import org.openbpm.base.db.model.query.DefaultQueryFilter;
import org.openbpm.base.rest.ControllerTools;
import org.openbpm.base.rest.util.RequestUtil;
import org.openbpm.bpm.api.engine.action.button.ButtonFactory;
import org.openbpm.bpm.api.model.nodedef.BpmNodeDef;
import org.openbpm.bpm.api.model.nodedef.Button;
import org.openbpm.bpm.api.service.BpmProcessDefService;
import org.openbpm.bpm.core.manager.BpmBusLinkManager;
import org.openbpm.bpm.core.manager.BpmInstanceManager;
import org.openbpm.bpm.core.model.BpmBusLink;
import org.openbpm.bpm.core.model.BpmInstance;
import org.openbpm.sys.util.ContextUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;


@RestController
@RequestMapping({"/bpm/cust"})
@Api(description = "定制流程接口")
public class EtechBpmController extends ControllerTools {


    @Resource
    BpmProcessDefService bpmProcessDefService;

    @Resource
    BpmInstanceManager bpmInstanceManager;

    @Resource
    BpmBusLinkManager bpmBusLinkManager;

    @RequestMapping({"getDefaultNodeBtns"})
    public List<Button> getDefaultNodeBtns(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<Button> buttonList = null;
        String nodeId = RequestUtil.getString(request, "nodeId");
        String defId = RequestUtil.getString(request, "defId");
        Boolean isDefault = Boolean.valueOf(RequestUtil.getBoolean(request, "isDefault", false));
        BpmNodeDef nodeDef = this.bpmProcessDefService.getBpmNodeDef(defId, nodeId);
        if ("instance_".equals(nodeId)) {
            buttonList = ButtonFactory.getInstanceButtons(isDefault.booleanValue());
        }
        buttonList = ButtonFactory.generateButtons(nodeDef, isDefault.booleanValue());
        // 项目定制
        if(isDefault && buttonList!=null){
            if(setBtnDefault==null){
                setBtnDefault = new HashSet<>();
                // 节点初始化按钮清单
                setBtnDefault.add("agree");
                setBtnDefault.add("flowImage");
                setBtnDefault.add("taskOpinion");
            }
            List<Button> buttonList2 = new ArrayList<>();
            for(Button btn: buttonList){
                String alias = btn.getAlias();
                if(!setBtnDefault.contains(alias)){
                    continue;
                }
                if("agree".equals(btn.getAlias())){
                    btn.setName("提交");
                }
                buttonList2.add(btn);
            }
            return buttonList2;
        }
        return buttonList;
    }

    private static Set<String> setBtnDefault;

    @RequestMapping(value = {"listInstanceJson_busLink"}, method = {RequestMethod.POST})
    @ApiOperation(value = "流程实例列表", notes = "获取流程实例列表-获取指定业务对象关联的流程实例列表， 如工程项目相关的流程列表")
//    @ApiImplicitParams({@ApiImplicitParam(dataType = "String", name = "bizCodes", value = "业务编码，多个逗号分隔"), @ApiImplicitParam(dataType = "String", name = "bizId", value = "业务ID"),@ApiImplicitParam(paramType = "form", dataType = "String", name = "sort", value = "排序字段"), @ApiImplicitParam(paramType = "form", dataType = "String", name = "order", value = "order", defaultValue = "ASC"), @ApiImplicitParam(paramType = "form", dataType = "String", name = "filter$VEQ", value = "其他过滤参数")})
    public ResultMsg<List<BpmInstance>> listJson_busLink(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<BpmInstance> bpmInstanceList = null;

        DefaultQueryFilter queryFilter2 = new DefaultQueryFilter();
        String bizCodes = RequestUtil.getString(request, "bizCodes");
        if(StringUtil.isNotEmpty(bizCodes)){
            queryFilter2.addFilter("biz_code_", bizCodes, QueryOP.IN );
        }
        String bizId = RequestUtil.getString(request, "bizId");
        if(StringUtil.isNotEmpty(bizId)){
            queryFilter2.addFilter("biz_id_", bizId, QueryOP.EQUAL );
        }
        List<BpmBusLink> links = bpmBusLinkManager.query(queryFilter2);
        if(links!=null && !links.isEmpty()){
            String instIds = null;
            for(BpmBusLink link: links){
                if(instIds==null){
                    instIds = link.getInstId();
                }else {
                    instIds += "," + link.getInstId();
                }
            }
            QueryFilter queryFilter = getQueryFilter(request);
            queryFilter.addFilter("id_", instIds, QueryOP.IN);
            bpmInstanceList = (Page)this.bpmInstanceManager.query(queryFilter);
        }
        return new ResultMsg(bpmInstanceList);
    }
}
